# 测试CodeQL API
Write-Host "测试CodeQL API..." -ForegroundColor Green

$baseUrl = "http://localhost:8085"

# 1. 检查服务是否运行
Write-Host "`n检查Data Service..." -ForegroundColor Yellow
try {
    $health = Invoke-WebRequest -Uri "$baseUrl/data/health" -Method GET -ErrorAction Stop
    Write-Host "✓ Data Service运行中" -ForegroundColor Green
} catch {
    Write-Host "✗ Data Service未运行，请先启动服务" -ForegroundColor Red
    Write-Host "  启动命令: cd data-service && mvn spring-boot:run" -ForegroundColor Gray
    exit 1
}

# 2. 列出可用查询
Write-Host "`n测试查询列表API..." -ForegroundColor Yellow
try {
    $queries = Invoke-RestMethod -Uri "$baseUrl/data/codeql/queries" -Method GET
    Write-Host "✓ 找到 $($queries.data.Count) 个可用查询" -ForegroundColor Green
    if ($queries.data.Count -gt 0) {
        Write-Host "`n查询列表（前5个）:" -ForegroundColor Cyan
        $queries.data | Select-Object -First 5 | ForEach-Object {
            Write-Host "  - $($_.name) (ID: $($_.id))" -ForegroundColor Gray
        }
    }
} catch {
    Write-Host "⚠ 查询列表API失败: $_" -ForegroundColor Yellow
}

# 3. 测试符号查询
Write-Host "`n测试符号查询API..." -ForegroundColor Yellow
$body = @{
    symbol = "LogUtil"
    context = @{
        filePath = "src/utils/log_util.cpp"
    }
    types = @("Function", "Class")
    maxResults = 5
} | ConvertTo-Json

try {
    $symbols = Invoke-RestMethod -Uri "$baseUrl/data/codeql/symbols/ast" `
        -Method POST `
        -Body $body `
        -ContentType "application/json"
    
    if ($symbols.code -eq 200) {
        Write-Host "✓ 符号查询成功" -ForegroundColor Green
        Write-Host "  找到 $($symbols.data.Count) 个符号" -ForegroundColor Cyan
        
        if ($symbols.data.Count -gt 0) {
            Write-Host "`n符号列表（前3个）:" -ForegroundColor Cyan
            $symbols.data | Select-Object -First 3 | ForEach-Object {
                Write-Host "  - $($_.name) ($($_.type))" -ForegroundColor Gray
                if ($_.qualifiedName) {
                    Write-Host "    完整名称: $($_.qualifiedName)" -ForegroundColor DarkGray
                }
            }
        }
    } else {
        Write-Host "⚠ 查询返回错误: $($symbols.message)" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠ 符号查询失败: $_" -ForegroundColor Yellow
    Write-Host "  可能原因:" -ForegroundColor Gray
    Write-Host "  1. CodeQL数据库未配置或路径错误" -ForegroundColor Gray
    Write-Host "  2. CodeQL CLI未安装或不在PATH中" -ForegroundColor Gray
    Write-Host "  3. 数据库损坏或不完整" -ForegroundColor Gray
}

# 4. 测试自定义查询（如果有可用查询）
if ($queries -and $queries.data.Count -gt 0) {
    Write-Host "`n测试自定义查询API..." -ForegroundColor Yellow
    $firstQuery = $queries.data[0]
    $queryBody = @{
        queryFile = $firstQuery.id
        parameters = @{}
        format = "json"
        timeout = 30
    } | ConvertTo-Json
    
    try {
        $result = Invoke-RestMethod -Uri "$baseUrl/data/codeql/query" `
            -Method POST `
            -Body $queryBody `
            -ContentType "application/json"
        
        if ($result.code -eq 200 -and $result.data.success) {
            Write-Host "✓ 自定义查询执行成功" -ForegroundColor Green
            Write-Host "  查询: $($result.data.queryName)" -ForegroundColor Cyan
            Write-Host "  结果数: $($result.data.count)" -ForegroundColor Cyan
            Write-Host "  执行时间: $($result.data.executionTime)ms" -ForegroundColor Cyan
        } else {
            Write-Host "⚠ 查询执行失败: $($result.data.error)" -ForegroundColor Yellow
        }
    } catch {
        Write-Host "⚠ 自定义查询失败: $_" -ForegroundColor Yellow
    }
}

Write-Host "`n测试完成！" -ForegroundColor Green

